<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/7/25
 * Time: 17:30
 * 军恋
 */
namespace app\api\controller;
use think\Db;
class Love extends Base {
    /**
     * 异地登录验证
     * @return string
     */
    public function __construct()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == 112){
            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
            exit;
        }elseif($auth == 110){
            echo json_encode(['status' => 302, 'msg' => $this->frost]);
            exit;
        }elseif($auth == 2111){
            echo json_encode(['status' => 303, 'msg' => $this->del]);
            exit;
        }
    }

    //发布军婚
    public function addLove(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名   2 匿名
        $is_publish = input('is_publish'); // 是否发布  1 发布   2 草稿箱
        $content = input('param.content');
        $content = $this->sensitive($content);
        $img = input('param.img');
        $data['post_content'] = $content;
        $data['post_img'] = $img;
        $data['post_create_time'] = date('Y-m-d H:i:s',time());
        $data['post_date'] = date('Y-m-d',time());
        $data['post_user_id'] = $user_id;
        $data['is_show_user'] = $is_show_user;
        $data['is_publish'] = $is_publish;
        $res = Db::table('up_love_post')->insertGetId($data);
        if ($res) {
            return json(array('status' => 200, 'msg' => "成功"));
        } else {
            return json(array('status' => 100, 'msg' => "失败"));
        }
    }

    //列表
    public function index(){
        extract(input());
        $page = input('param.page')?input('param.page'):1;
        $num = 10;
        $limit = $page-1;
        $res = Db::table('up_love_post')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.post_user_id','left')
            ->where('post_status', 1)
            ->where('is_publish', 1)
            ->order('a.post_create_time desc')
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->limit($limit*$num,$num)
            ->select();
        foreach ($res as $key=>&$vo){
            $where['user_id'] = $user_id;
            $where['like_user_id'] = $vo['post_user_id'];
            $info = Db::table('up_like')->where($where)->find();
            if($info['like_id']!=''){
                $vo['is_like'] = 1;
            }else{
                $vo['is_like'] = 2;
            }
            $where1['type'] = 3;
            $where1['f_id'] = $vo['post_id'];
            $Num = Db::table('up_comment')->where($where1)->count();
            $vo['count'] = $Num;
        }
        return  json(array('status'=>200,'msg'=>$res));
    }

    //举报
    public function report(){
        extract(input());
        $r_type_id = input('param.id');
        $data['r_user_id'] = $user_id;
        $data['report_user_id'] = $report_user_id;
        $data['report_time'] = date('Y-m-d H:i:s',time());
        $data['report_type'] = 5;
        $data['r_type_id'] = $r_type_id;
        $data['report_intro'] = input('param.report_intro');
        $res = Db::table('up_report')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"成功"));
        }else{
            return  json(array('status'=>100,'msg'=>"失败"));
        }
    }

    //删除问题
    public function del(){
        extract(input());
        $id = input('param.id');
        $info = Db::table('up_love_post')->where('post_id',$id)->find();
        if($info['post_user_id']!=$user_id){
            return  json(array('status'=>100,'msg'=>"不能删除不属于自己的"));
        }else{
            $where['f_id'] = $id;
            $where['type'] = 3;
            $info1 = Db::table('up_comment')->where($where)->count();
            if($info1>=1){
                Db::table('up_comment')->where($where)->delete();
            }
            $res = Db::table('up_love_post')->where('post_id',$id)->delete();
            if($res){
                return  json(array('status'=>200,'msg'=>"删除成功"));
            }else{
                return  json(array('status'=>100,'msg'=>"删除失败"));
            }
        }
    }
    //关注
    public function like(){
        extract(input());
        $other_user = input('param.other_user');
        $where['user_id'] = $user_id;
        $where['like_user_id'] = $other_user;
        $aa = Db::table('up_like')->where($where)->find();
        if($aa['like_id']!=''){//证明存在
            $res = Db::table('up_like')->where($where)->delete();
        }else{
            $data['like_time'] = date('Y-m-d H:i:s',time());
            $data['user_id'] = $user_id;
            $data['like_user_id'] = $other_user;
            $res = Db::table('up_like')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "成功"));
        } else {
            return json(array('status' => 200, 'msg' => "失败"));
        }
    }
    //详情
    public function loveDetail(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num1 = 10;
        $limit = $page-1;
        $where['post_id'] = $id;
        $res = Db::table('up_love_post')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.post_user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,uuu.user_id,a.*')
            ->find();
        if(!$res){
            return json(['status' => 0, 'msg' => '数据不存在']);
        }
        //查询是否收藏这个资讯
        $whereMap4['collection_f_id'] = $id;
        $whereMap4['collection_type'] =5;
        $whereMap4['collection_user_id'] = $user_id;
        $collection = Db::table('up_collection')->where($whereMap4)->find();
        if($collection['collection_id']!=''){
            $res['is_collection'] = 1;
        }else{
            $res['is_collection'] = 2;
        }
        //统计多少评论
        $where1['f_id'] = $id;
        $where1['type'] = 3;
        $num = Db::table('up_comment')->alias('a')
            ->where($where1)
            ->count();
        $res['share_num'] = $num;
        //查询评论
        $where2['f_id'] =$id;
        $where2['type'] =3;
        //查询评论数据
        $res1 = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where2)
            ->field('uuu.user_username,uuu.user_image,uuu.user_id,a.comment,a.comment_id,a.time,a.is_show_user')
            ->limit($limit*$num1,$num1)
            ->select();
        foreach ($res1 as $key=>&$vo) {
            //评论有多少回复
            $count = Db::table('up_comment_reply')->where('f_id', $vo['comment_id'])->count();
            $vo['count'] = $count;
            //评论有多少点赞
            $map['give_f_id'] = $vo['comment_id'];
            $map['give_type'] = 13;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;

            $map1['give_type'] = 13;
            $map1['give_f_id'] = $vo['comment_id'];
            $map1['give_user_id'] = $user_id;
            $info = Db::table('up_comment_give')->where($map1)->find();
            if ($info['give_id'] != '') {//点赞过
                $vo['is_dz'] = 1;
            } else {
                $vo['is_dz'] = 2;
            }
        }

        //查询是否关注
        $whereMap1['user_id'] = $user_id;
        $whereMap1['like_user_id'] = $res['post_user_id'];
        $info2 = Db::table('up_like')->where($whereMap1)->find();
        if($info2['like_id']!=''){
            $res['is_like'] = 1;
        }else{
            $res['is_like'] = 2;
        }
        $res['data']=$res1;
        return  json(array('status'=>200,'msg'=>$res));
    }
    //收藏
    public function collection(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $id = input('param.id');
        $data['collection_f_id'] = $id;
        $data['collection_user_id'] = $user_id;
        $data['collection_type'] = 5;
        $res1 = Db::table('up_collection')->where($data)->find();
        if($res1['collection_id']!=''){
            $res= Db::table('up_collection')->where($data)->delete();
        }else{
            $data['collection_type']=5;
            $res= Db::table('up_collection')->insertGetId($data);
        }
        if ($res) {
            return json(array('status' => 200, 'msg' => "收藏成功"));
        } else {
            return json(array('status' => 200, 'msg' => "收藏失败"));
        }
    }
    //评论添加
    public function comment(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名    1 不匿名    2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content);
        $video_id = input('param.love_id');
        $data['f_id'] = $video_id;
        $data['comment'] = $content;
        $data['time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['type'] = 3;
        $data['f_user_id'] = input('param.other_user');
        $data['is_show_user'] = $is_show_user;
        //todo
        $res = Db::table('up_comment')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }

    //评论点赞
    public function Dz(){
        // type  1评论回复点赞   2评论回复点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 13;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2){
            $data['give_user_id'] = $user_id;
            $data['give_type'] =13;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }
    //所有回复
    public function allLove(){
        extract(input());
        $id = input('param.id');
        $page = input('param.page')?input('param.page'):1;
        $num1 = 10;
        $limit = $page-1;
        $where['comment_id'] = $id;
        $res = Db::table('up_comment')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where)
            ->field('uuu.user_username,uuu.user_image,uuu.user_id,a.*')
            ->find();
        //点赞数量
        $map2['give_type'] = 13;
        $map2['give_f_id'] = $id;
        $numCount = Db::table('up_comment_give')->where($map2)->count();
        $res['numCount'] = $numCount;
        //自己是否点赞
        $map3['give_type'] = 13;
        $map3['give_f_id'] = $id;
        $map3['give_user_id'] = $user_id;
        $info4 = Db::table('up_comment_give')->where($map3)->find();
        if ($info4['give_id'] != '') {//点赞过
            $res['my_is_dz'] = 1;
        } else {
            $res['my_is_dz'] = 2;
        }
        //统计多少评论
        $where1['f_id'] = $id;
        $num = Db::table('up_comment_reply')->alias('a')
            ->where($where1)
            ->count();
        $res['share_num'] = $num;
        //查询评论数据
        $where2['a.f_id'] =$id;
        $res1 = Db::table('up_comment_reply')->alias('a')
            ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
            ->where($where2)
            ->field('uuu.user_username,uuu.user_image,a.*')
            ->limit($limit*$num1,$num1)
            ->select();
        foreach ($res1 as $key=>&$vo) {
            //评论有多少点赞
            $map['give_f_id'] = $vo['comment_reply_id'];
            $map['give_type'] = 14;
            $count1 = Db::table('up_comment_give')->where($map)->count();
            $vo['count1'] = $count1;
            //自己是否点赞
            $map1['give_type'] = 14;
            $map1['give_f_id'] = $vo['comment_reply_id'];
            $map1['give_user_id'] = $user_id;
            $info = Db::table('up_comment_give')->where($map1)->find();
            if ($info['give_id'] != '') {//点赞过
                $vo['is_dz'] = 1;
            } else {
                $vo['is_dz'] = 2;
            }
        }
        $res['data']=$res1;
        return  json(array('status'=>200,'msg'=>$res));
    }

    //评论回复点赞
    public function pingDz(){
        // type  1评论回复点赞   2评论回复取消点赞
        extract(input());
        $type = input('param.type');
        if($type==1) {
            $data['give_user_id'] = $user_id;
            $data['give_time'] = date('Y-m-d H:i:s', time());
            $data['give_type'] = 14;
            $data['give_f_id'] = input('param.id');
            $data['give_f_f_id'] = input('param.pl_id');
            $data['give_by_user_id'] = input('param.other_user');
            $res = Db::table('up_comment_give')->insertGetId($data);
            if ($res) {
                return json(array('status' => 200, 'msg' => "点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "点赞失败"));
            }
        }elseif ($type==2) {
            $data['give_user_id'] = $user_id;
            $data['give_type'] = 14;
            $data['give_f_id'] = input('param.id');
            $res = Db::table('up_comment_give')->where($data)->delete();
            if ($res) {
                return json(array('status' => 200, 'msg' => "取消点赞成功"));
            } else {
                return json(array('status' => 200, 'msg' => "取消点赞失败"));
            }
        }
    }

    //评论回复添加
    public function answer(){
        extract(input());
        $is_show_user = input('is_show_user'); // 是否匿名   1 不匿名   2 匿名
        $content = input('param.content');
        $content = $this->sensitive($content);
        $comment_id = input('param.id');
        $love_id = input('love_id');
        $data['f_id'] = $comment_id;
        $data['f_f_id'] = $love_id;
        $data['comment_reply_content'] = $content;
        $data['comment_time'] = date('Y-m-d H:i:s',time());
        $data['user_id'] = $user_id;
        $data['by_user_id'] = input('param.other_user');
        $data['is_show_user'] = $is_show_user;
        $res = Db::table('up_comment_reply')->insertGetId($data);
        if($res){
            return  json(array('status'=>200,'msg'=>"发布成功"));
        }else{
            return  json(array('status'=>200,'msg'=>"发布失败"));
        }
    }
}